package com.demon.minio.enums;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author : wmwu
 * @data : 2022/6/23 16:48
 */
public interface ICommEnum<T extends Serializable> {
    /**
     * 获取枚举Id
     *
     * @return
     */
    default T getEnumId() {
        return (T) EnumBaseInfo.ENUMS.get(this).getId();
    }

    /**
     * 获取枚举描述信息
     *
     * @return
     */
    default String getDesc() {
        return EnumBaseInfo.ENUMS.get(this).getDesc();
    }

    /**
     * 获取枚举描述信息
     *
     * @return
     */
    default void addEnum(ICommEnum<T> thisEnum, T id, String desc) {
        EnumBaseInfo.ENUMS.put(thisEnum, new EnumBaseInfo<T>(id, desc));
    }


    class EnumBaseInfo<T extends Serializable> implements Serializable {
        static final long serialVersionUID = 2119128648123039388L;
        static final Map<ICommEnum<?>, EnumBaseInfo<?>> ENUMS = new HashMap<>();
        private T id;
        private String desc;

        public T getId() {
            return id;
        }

        public String getDesc() {
            return desc;
        }

        public EnumBaseInfo() {
        }

        public EnumBaseInfo(T id, String desc) {
            this.id = id;
            this.desc = desc;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            EnumBaseInfo<?> that = (EnumBaseInfo<?>) o;
            if (!id.equals(that.id)) {
                return false;
            }
            return desc.equals(that.desc);
        }

        @Override
        public int hashCode() {
            return Objects.hash(id, desc);
        }

        @Override
        public String toString() {
            return "EnumBaseInfo{" +
                    "id=" + id +
                    ", desc='" + desc + '\'' +
                    '}';
        }
    }
}



